﻿// Copyright (c) Microsoft Corporation.
// This sample code is provided "as is" without warranty of any kind. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QueryServiceApplication.QueryServiceReference;
using System.Data;

namespace QueryServiceApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Build query
            var queryName = "CarsQuery";
            var tableName = "FMIncCars";
            var query = new QueryMetadata()
            {
                Name = queryName,
                DataSources = new[] { new QueryDataSourceMetadata()
                    {
                        Name = tableName,
                        Table = tableName,
                        Enabled = true,
                        Fields = new QueryFieldMetadata[] 
                        { 
                            new QueryDataFieldMetadata() { FieldName = "Make", TableName = tableName, SelectionField = SelectionField.Database },
                            new QueryDataFieldMetadata() { FieldName = "Model", TableName = tableName, SelectionField = SelectionField.Database },
                            new QueryDataFieldMetadata() { FieldName = "VIN", TableName = tableName, SelectionField = SelectionField.Database },
                            new QueryEnumSymbolFieldMetadata() { EnumFieldName = "Status" },
                        }
                    }
                }
            };

            // Execute query and get results
            var client = new QueryServiceClient();
            Paging paging = null;
            var dataSet = client.ExecuteQuery(query, ref paging);
            var table = dataSet.Tables[0];

            // Print header
            foreach(DataColumn column in table.Columns)
            {
                Console.Write(column.ColumnName + ", ");
            }
            Console.WriteLine();

            // Print rows
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    Console.Write(row[column.ColumnName] + ", ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}
